package com.guigu.youxuan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.youxuan.common.utils.PageUtils;
import com.guigu.youxuan.mapper.CartInfoMapper;
import com.guigu.youxuan.pojo.BackUser;
import com.guigu.youxuan.pojo.CartInfo;
import com.guigu.youxuan.pojo.dto.CartInfoDto;
import com.guigu.youxuan.service.ICartInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liweixin
 * @since 2024-05-15
 */
@Service
public class CartInfoServiceImpl extends ServiceImpl<CartInfoMapper, CartInfo> implements ICartInfoService {
    @Autowired
    private CartInfoMapper cartInfoMapper;

    @Override
    public PageUtils<CartInfoDto> cartInfoList(int sysUsersId,int pageNo, int pageSize) {
        // 设置分页参数
        PageHelper.startPage(pageNo, pageSize);
        List<CartInfoDto> cartInfoDtoList = cartInfoMapper.GoodsInfoListPage(sysUsersId);
        PageInfo<CartInfoDto> pageInfo = new PageInfo<>(cartInfoDtoList);
        // 转换：将PageInfo转换成PageUtils
        PageUtils<CartInfoDto> pageUtils = new PageUtils<>();
        pageUtils.setTotal(pageInfo.getTotal());
        pageUtils.setRows(pageInfo.getList());
        return pageUtils;
    }

    /**
     * 购物车数量
     * @param sysUsersId
     * @return
     */
    @Override
    public String selCarts(int sysUsersId) {
        String selCarts=cartInfoMapper.selCarts(sysUsersId);
        return selCarts;
    }

    /**
     * 如果购物车中已经存在该商品，则更新数量
     * @param cartId
     */
    @Override
    public void delCartInfo(int cartId) {
        cartInfoMapper.delCartInfo(cartId);
    }

    @Override
    public void addToCart(int sysUsersId, int goodsId, int goodsCount) {
        CartInfo cartInfo = new CartInfo();
        cartInfo.setSysUsersId(sysUsersId);
        cartInfo.setGoodsId(goodsId);
        cartInfo.setGoodsCount(goodsCount);
        cartInfo.setChecked(0);
        this.save(cartInfo);
    }

    /**
     * 通过用户ID 商品ID查询购物车中是否已经存在该商品
     * @param sysUsersId
     * @param goodsId
     * @return
     */
    @Override
    public CartInfo getCartItem(int sysUsersId, int goodsId) {
        return cartInfoMapper.selectCartItem(sysUsersId, goodsId);
    }

    /**
     * 如果购物车中已经存在该商品，则更新数量
     * @param goodsId
     * @param goodsCount
     */
    @Override
    public void updateCartCount(int goodsId, int goodsCount) {
        cartInfoMapper.updateCartCount(goodsId,goodsCount);
    }
}
